<?php


namespace app\home\controller;


use app\common\controller\Base as BaseCommon;
use app\common\model\ClientModel;
use app\common\model\StaffModel;
use app\common\model\StaffTicketModel;
use think\App;
use think\facade\Session;
use utils\Jwt;

class Base extends BaseCommon
{
    public $notLogin = ['Login'];
    public $userInfo = [];
    public $uid;

    function __construct(App $app)
    {
        parent::__construct($app);

        //测试登录
        $this->debugLogin();

        //如果需要登录  检查登录
        if(!in_array(request()->controller(),$this->notLogin)){
            $ticket = input('ticket')?:Session::get('ticket') ;
//            dump($_SESSION);exit;
            if(empty($ticket)){
                $this->redirect(url('Login/index'));
//                $this->error('请先登录！',url('Login/index'));
            }
            $userInfo = Jwt::verifyToken($ticket);
//            dump($userInfo);exit;
            if(empty($userInfo)){
                $this->error('您的登录已失效，请重新登录！'.$ticket,url('Login/index'));
            }
            $uid = $userInfo['id'];

            //如果缓存里面没有  就从数据库里面读取
//            if(empty(Session::get('staffUserInfo'))){
                if($userInfo['role']==1){
                    $uid = $userInfo['uid'];//客服id
//                    $_userinfo = StaffModel::getInfo($uid);
                    $_userinfo = $userInfo;
                }else{
                    $_userinfo = $userInfo;

//                    $_userinfo = ClientModel::getInfo($uid);
                    $_userinfo['role']=2;
                }
//                Session::set('staffUserInfo', $_userinfo);
//            }
            $this->userInfo = $_userinfo;
//            $this->userInfo = Session::get('staffUserInfo');
            $this->uid = $uid;
        }
    }

    //快捷验证
    function validate(array $data, $validate='', array $message = [], bool $batch = false)
    {
        if($validate==''){
            $validate = $this->request->controller().'.'.$this->request->action();
        }
        return parent::validate($data, $validate, $message, $batch); // TODO: Change the autogenerated stub
    }

    //测试登录
    function debugLogin(){
        $param = $this->request->param();
        $debug = $param['debug'];
        $id = $param['id'];
        if($debug==1 && $id){
            $info = StaffModel::getInfo($id);
            if($info)
                StaffModel::setTicket($info);
        }
    }

}